/**
 * <AiRmob-Framework; A simple augmented reality framework for android >
    Copyright (C) <2012>  <AiRmob Team>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package rendercomponents;

import java.util.LinkedList;

public class ModelAnimation extends Object {
	protected LinkedList<ModelCustom> frames;
	protected int start = 0;
	protected int duration = 0;
	protected int localFrameCount = 0;
	protected int delayPerFrame = 0;
	protected int FPFcount = 0;
	protected boolean loop = true;
	protected boolean paused = false;
	protected boolean stopped = false;
	protected boolean firstFrameAfterPaused = false;
	protected boolean firstFrameAfterUnpaused = false;
	protected float timePaused = 0;

	// Animation by Time
	protected boolean byTime = false;
	protected float startTime = 0;
	protected float currentTime = 0;
	protected float time = 0;
	protected float fps = 0;

	/**
	 * Constructs a frame-based modelAnimation out of given source.
	 * 
	 * @param r
	 *            the used Renderer
	 * @param path
	 *            the path from where the animation shall be imported
	 * @param type
	 *            the type of file used
	 * @param count
	 *            the number of files that shall be imported
	 * @param stride
	 *            the stride which will be used to determine which files will be
	 *            imported
	 */
	public ModelAnimation(Renderer r, String path, ModelFileTypes type,
			int count, int stride) {
		this.construct(r, path, type, count, stride, false, 0);
	}

	/**
	 * Constructs a modelAnimation out of given source.
	 * 
	 * @param r
	 *            the used Renderer
	 * @param path
	 *            the path from where the animation shall be imported
	 * @param type
	 *            the type of file used
	 * @param count
	 *            the number of files that shall be imported
	 * @param stride
	 *            the stride which will be used to determine which files will be
	 *            imported
	 * @param byTime
	 *            if set to true the animation will be measured by time
	 * @param seconds
	 *            the seconds the time-based animation shall last
	 */
	public ModelAnimation(Renderer r, String path, ModelFileTypes type,
			int count, int stride, boolean byTime, float seconds) {
		this.construct(r, path, type, count, stride, byTime, seconds);
	}

	private void construct(Renderer r, String name, ModelFileTypes type,
			int count, int stride, boolean byTime, float seconds) {
		this.frames = new LinkedList<ModelCustom>();
		this.type = 6;
		this.duration = 0;
		this.time = seconds;
		this.byTime = byTime;
		if (byTime)
			r.measureTimeForAnimation = true;
		if (stride <= 0)
			stride = 1;
		if (seconds > 0)
			this.fps = (count / stride) / seconds;

		name += "_";

		for (int i = 1; i < count; i += stride) {
			this.duration++;
			String path = name;
			if (i < 10)
				path += "00000" + i + ".obj";
			if (i >= 10 && i < 100)
				path += "0000" + i + ".obj";
			if (i >= 100 && i < 1000)
				path += "000" + i + ".obj";
			if (i >= 1000 && i < 10000)
				path += "00" + i + ".obj";
			if (i >= 10000 && i < 100000)
				path += "0" + i + ".obj";
			ModelCustom frame = new ModelCustom(r, path, type);
			frame.name = path + i;
			frames.add(frame);
		}
	}

	/**
	 * 
	 * @param time
	 *            the duration the animation shall last in frames.
	 */
	public void setDuration(int time) {
		this.duration = time;
		this.loop = false;
	}

	/**
	 * 
	 * @param frame
	 *            the frame on which the animation shall start automatically
	 */
	public void setStart(int frame) {
		this.start = frame;
	}

	/**
	 * Starts this animation immediately.
	 * 
	 * @param r
	 *            the used renderer
	 */
	public void startNow(Renderer r) {
		this.start = r.frameCount + 1;
	}

	/**
	 * If paused the animation will be unpaused and vice versa.
	 */
	public void pauseUnpause() {
		if (!paused) {
			this.paused = true;
			this.firstFrameAfterPaused = true;
		} else {
			this.paused = false;
			this.firstFrameAfterUnpaused = true;
		}
	}

	// ////////////////////////////////////////////////////////
	// /Getters and setters (auto-generated)
	// ////////////////////////////////////////////////////////

	public LinkedList<ModelCustom> getFrames() {
		return frames;
	}

	public void setFrames(LinkedList<ModelCustom> frames) {
		this.frames = frames;
	}

	public int getDelayPerFrame() {
		return delayPerFrame;
	}

	public void setDelayPerFrame(int delayPerFrame) {
		this.delayPerFrame = delayPerFrame;
	}

	public boolean isLoop() {
		return loop;
	}

	public void setLoop(boolean loop) {
		this.loop = loop;
	}

	public boolean isStopped() {
		return stopped;
	}

	public void setStopped(boolean stopped) {
		this.stopped = stopped;
	}

}
